package com.hqj;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AutoMybaitPlus {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
        System.out.println("===========开始执行自动生成,"+sdf.format(new Date())+"===========");
        createGenerator();
        System.out.println("===========执行自动生成完成,"+sdf.format(new Date())+"===========");
    }

    //代码构造器
    public static void createGenerator(){
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(getConfig());
        ag.setDataSource(getDataSource());
        ag.setPackageInfo(getPackage());
        ag.setTemplate(getTemplate());
        ag.setStrategy(getStrategy());
        //执行生成
        ag.execute();
    }

    public static GlobalConfig getConfig(){
        GlobalConfig gc = new GlobalConfig();
        String parentRoot = "D:\\Workspaces\\Idea\\TheCodeOfJava202012\\SpiderMan";
        String secondRoot = "/src/main/java";

        gc.setOutputDir(parentRoot + secondRoot);
        gc.setAuthor("HQJ");
        gc.setFileOverride(false);
        gc.setOpen(false);
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
        //设置文件生成的名称
        gc.setMapperName("%sDao");
        gc.setServiceImplName("%sServiceI");
        gc.setControllerName("%sController");
        return gc;
    }

    public static DataSourceConfig getDataSource(){
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/spider_man2021?characterEncoding=utf-8");
        dsc.setUsername("root");
        dsc.setPassword("bts.2015");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        return dsc;
    }

    public static PackageConfig getPackage(){
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.spider");
        pc.setController("controller");
        pc.setMapper("dao");
        pc.setServiceImpl("service");
        pc.setEntity("domain");
        return pc;
    }

    public static TemplateConfig getTemplate(){
        TemplateConfig tc = new TemplateConfig();
        tc.setService(null);
        tc.setXml(null);
        return tc;
    }

    public static StrategyConfig getStrategy(){
        StrategyConfig sc = new StrategyConfig();

        sc.setInclude("s_spider");
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        sc.setEntityLombokModel(true);
        sc.setRestControllerStyle(false);
        sc.setTablePrefix("s_");
        return sc;
    }



}
